<template>
  <div>
    <dami-dialog @confirm="confirm" @cancel="cancel">
      <el-form :inline="true" class="demo-form-inline">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="标题">
            <el-input v-model="options.Title" placeholder="标题"></el-input>
          </el-form-item>
        </el-form>
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="内容">
            <el-input v-model="options.SubStance" placeholder="内容"></el-input>
          </el-form-item>
        </el-form>
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="Url">
            <el-input v-model="options.Url" placeholder="Url"></el-input>
          </el-form-item>
        </el-form>
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="图标">
            <upload ref='upload' :action-url='imgUploadPath' :multiple='false'></upload>
          </el-form-item>
        </el-form>
      </el-form>
    </dami-dialog>
  </div>
</template>

<script type="text/javascript">
import upload from  '../../../component/upload';
import SYS_Message_update from  '../../../API/SYS_Message_update.js';
const config = require('../../../../config');
const imgUploadPath = !!process.env  ? config.dev.imgUploadPath : config.build.imgUploadPath;
const imgPath = !!process.env  ? config.dev.imgPath : config.build.imgPath;


  export default {
    data() {
       return {
         imgUploadPath:imgUploadPath,
         options:{
           Title:'',
           SubStance:'',
           Url:'',
           ImageUrl:''
         }
       }
    },
    components:{
      'upload':upload
    },
    props:['edit-id','name','view-state','item'],
    watch:{
      viewState(val){
        if(!!val){
          this.options.Title = this.item.Title;
          this.options.SubStance = this.item.SubStance;
          this.options.Url = this.item.Url;
          this.options.ImageUrl = this.item.ImageUrl;
          this.$refs.upload.initData([imgPath+this.options.ImageUrl])
        }
      }
    },
    mounted:function(){
      this.options.Title = this.item.Title;
      this.options.SubStance = this.item.SubStance;
      this.options.Url = this.item.Url;
      this.options.ImageUrl = this.item.ImageUrl;
      this.$refs.upload.initData([imgPath+this.options.ImageUrl])

    },
    methods:{
      cancel(){
        this.$emit("cancel");
        this.$refs.upload.reset();
      },
      confirm(){
        if(!this.editId){
          this.$Message({
              message:"请传递操作ID"
          });
          return;
        }

        this.$refs.upload.upload(data => {
          if(data.res == "0"){
            this.$Message({
                message:data.mes
            });
            return;
          }
          if(data.res == "-1"){
            this.options.ImageUrl = "";//没有选择图片 无需更改
          }else{
            let imgList = data.imgList[0];
            let imgPath = imgList.value;
            this.options.ImageUrl = imgPath;
          }
          let parm = {};
          parm.Id = this.editId;
          Object.assign(parm,this.options);
          SYS_Message_update(parm).then(data => {
            this.$Message({
                message:"操作成功"
            });
            this.$emit("confirm");
            this.$refs.upload.reset();
          }).catch(message => {
            this.$Message({
                message
            });
          });

        });


      }
    }
  }
</script>
